#!/usr/bin python
# -*- coding: utf-8 -*-
# @Time    : 2024/4/23 14:11
# @Author  : hhr
# 检测超期流程

import pymysql


class AutomaticScheduling():
    def __init__(self, development):
        self.conn = pymysql.connect(host="localhost", port=3306, user='root', password='123456' if development == 'test' else 'jsyy2021', db="administration",
                                    use_unicode=True, charset='utf8')
        self.cur = self.conn.cursor()

    def search_data(self):
        select_sql = "SELECT id, state, responsible_person_id FROM `supervision_inspection_worksupervisehandle` WHERE NOW() > end_time AND transfer_supervision_office='0' AND state IN ('one', 'two')"
        self.cur.execute(select_sql)
        search_data = self.cur.fetchall()
        for i in search_data:
            select_sql1 = "SELECT head_people_id FROM `administration`.`administrative_company` WHERE `id` = '18'"
            self.cur.execute(select_sql1)
            com_18 = self.cur.fetchall()

            insert_sql = f"INSERT INTO `supervision_inspection_overdueflow` (create_time, update_time, parent_id_id, deadline, process_owner_id) VALUES(NOW(), NOW(), {i[0]}, '0', {com_18[0][0]})"
            self.cur.execute(insert_sql)
            self.cur.execute("SELECT LAST_INSERT_ID()")
            result = self.cur.fetchone()
            last_id = result[0]
            self.conn.commit()

            insert_sql2 = f"INSERT INTO `supervision_inspection_worksupervisehandlerecord` (create_time, update_time, state, node, audit_note, audit_people_id, parent_id_id, file_path) VALUES(NOW(), NOW(), 'unaudit', 'one', '', '{i[2]}', '{i[0]}', '')"
            self.cur.execute(insert_sql2)
            self.conn.commit()

            insert_sql1 = f"INSERT INTO `supervision_inspection_overdueflow_current_audit_users` (overdueflow_id, user_id) VALUES({last_id}, {com_18[0][0]})"
            self.cur.execute(insert_sql1)
            self.conn.commit()

            updata_sql = f"UPDATE supervision_inspection_worksupervisehandle SET transfer_supervision_office='1' WHERE id={i[0]}"
            self.cur.execute(updata_sql)
            self.conn.commit()

            dalete_sql = f"DELETE FROM supervision_inspection_worksupervisehandle_current_audit_users WHERE worksupervisehandle_id={i[0]}"
            self.cur.execute(dalete_sql)
            self.conn.commit()


    def delete_reviewer(self):
        select_sql = "SELECT work_id_id FROM `administration`.`supervision_inspection_deadlinenotification` WHERE NOW() >= end_deadline_time"
        self.cur.execute(select_sql)
        search_data = self.cur.fetchall()
        for i in search_data:
            dalete_sql = f"DELETE FROM `supervision_inspection_worksupervisehandle_current_audit_users` WHERE worksupervisehandle_id={i[0]}"
            self.cur.execute(dalete_sql)
            self.conn.commit()


    def main_fun(self):
        self.search_data()
        self.delete_reviewer()
        self.cur.close()
        self.conn.close()



if __name__ == '__main__':
    development = 'develop'
    AutomaticScheduling(development).search_data()



